package com.huang.springboot.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author yili_gi
 */
@Configuration
public class ShiroConfig {

    @Bean
    public UserRealm getUserRealm(){
        return new UserRealm();
    }

    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Autowired UserRealm userRealm){

        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;

    }

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(
            @Autowired DefaultWebSecurityManager getDefaultWebSecurityManager){

        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(getDefaultWebSecurityManager);

        Map<String,String> filterMap = new LinkedHashMap<>();
        filterMap.put("/emp/delete","perms[user:admin]");
        filterMap.put("/emp/update","perms[user:admin]");
        filterMap.put("/emp/add","perms[user:admin]");
        filterMap.put("/dep/*","perms[user:admin]");
        filterMap.put("/emp/**","authc");
        filterMap.put("/loginOut","logout");
        bean.setFilterChainDefinitionMap(filterMap);
        bean.setLoginUrl("/");
        return bean;
    }

}
